package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.WorkflowProcess;
import com.qidianit.mes.pro.mapper.WorkflowProcessMapper;
import com.qidianit.mes.pro.service.WorkflowProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: YanJiRan
 * @Date: 2025/9/18 - 下午8:22
 */
@Service
public class WorkflowProcessServiceImpl implements WorkflowProcessService {
    @Autowired
    private WorkflowProcessMapper workflowProcessMapper;
    @Override
    public Result<PageInfo<WorkflowProcess>> selectList(WorkflowProcess workflowProcess, int page, int size) {
        //开启分页
        PageHelper.startPage(page,size);
        List<WorkflowProcess> workflowProcesses = workflowProcessMapper.selectList(workflowProcess);
        return Result.success(new PageInfo<>(workflowProcesses));
    }

    @Override
    public Result<WorkflowProcess> saveWorkflowProcess(WorkflowProcess workflowProcess) {
        //同一工艺下是否已存在相同的工序ID或步骤顺序
        WorkflowProcess checkCondition = new WorkflowProcess();
        checkCondition.setWorkflowId(workflowProcess.getWorkflowId());
        //同一工艺下的所有工序
        List<WorkflowProcess> processes = workflowProcessMapper.selectList(checkCondition);
        for (WorkflowProcess process : processes) {
            //就是本身id
            if (!process.getId().equals(workflowProcess.getId())) {
                //步骤顺序是否重复
                if (process.getStepOrder().equals(workflowProcess.getStepOrder())) {
                    return Result.error("工序序号[" + workflowProcess.getStepOrder() + "]已经存在");
                }
                //工序ID是否重复
                if (process.getProcessId().equals(workflowProcess.getProcessId())) {
                    //获取工序名称
                    String processName = "本工序";
                    if (process.getProcess() != null && process.getProcess().getProcessName() != null) {
                        processName = process.getProcess().getProcessName();
                    }
                    return Result.error("工序" + processName + "已经存在");
                }
            }
        }
        return workflowProcessMapper.saveWorkflow(workflowProcess) == 1 ? Result.success() : Result.error("操作失败");
    }

    @Override
    public Result<WorkflowProcess> selectById(Long workflowProcessId) {
        return Result.success(workflowProcessMapper.selectById(workflowProcessId));
    }

    @Override
    public Result updateById(WorkflowProcess workflowProcess) {
        //同一工艺下是否已存在相同的工序ID或步骤顺序
        WorkflowProcess checkCondition = new WorkflowProcess();
        checkCondition.setWorkflowId(workflowProcess.getWorkflowId());

        //同一工艺下的所有工序
        List<WorkflowProcess> processes = workflowProcessMapper.selectList(checkCondition);

        for (WorkflowProcess process : processes) {
            //就是本身id
            if (!process.getId().equals(workflowProcess.getId())) {
                //步骤顺序是否重复
                if (process.getStepOrder().equals(workflowProcess.getStepOrder())) {
                    return Result.error("工序序号[" + workflowProcess.getStepOrder() + "]已经存在");
                }
                //工序ID是否重复
                if (process.getProcessId().equals(workflowProcess.getProcessId())) {
                    //获取工序名称
                    String processName = "本工序";
                    if (process.getProcess() != null && process.getProcess().getProcessName() != null) {
                        processName = process.getProcess().getProcessName();
                    }
                    return Result.error("工序" + processName + "已经存在");
                }
            }
        }

        return workflowProcessMapper.updateById(workflowProcess) == 1 ? Result.success() : Result.error("操作失败");
    }

    @Override
    public Result deleteById(Long workflowProcessId) {
        return workflowProcessMapper.deleteById(workflowProcessId) == 1 ? Result.success() : Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return workflowProcessMapper.batchDelete(ids) > 0 ? Result.success() : Result.error("操作失败");
    }
}
